package main.Trees;

import main.Link.SQLLink;
import main.SQL.SQLTaskRsrc;

import javax.swing.tree.DefaultMutableTreeNode;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class ProjectNode extends DefaultMutableTreeNode {

    private String name, shortName, dbName, taskCodeValue, pvValue, uomValue, high, taskUse, WDCode, section, sector, block, subBlock, constrCode;
    private int id, wbsId, parentId;
    private Boolean isTask, isSmString;
    private Double taskCompletePct;
    private Date startDate, finishDate;

    private ArrayList<SQLTaskRsrc> rsrc = new ArrayList<>();
    private HashMap<String, SQLLink> links = new HashMap<>();

    private int wbsLevel;

    //Атрибуты проекта
    private String projCode;

    //Сметные данные
    private Boolean isPV;
    private String smrNum, smrName, smrSmNum, smrUnit;
    private Double smrSmrVol, smrLinkVol, smrMrVol, PV, smrTVol, smrMechVol;

    public ProjectNode(ResultSet rs, String type) throws SQLException {

        if (type.equals("task")) {

            name = rs.getString("task_name");
            this.shortName = rs.getString("task_code");
            this.id = rs.getInt("task_id");
            this.parentId = rs.getInt("wbs_id");
            this.taskCompletePct = rs.getDouble("task_complete_pct");
            this.taskCodeValue = rs.getString("act_code_value");
            this.startDate = rs.getDate("actual_start_date");
            this.finishDate = rs.getDate("actual_finish_date");
            this.isSmString = false;
            this.high = "";
            this.taskUse = "";

            this.pvValue = rs.getString( "pv_value" );
            this.uomValue = rs.getString( "uom_value" );
            this.WDCode = rs.getString( "wd_code" );

            this.isTask = true;
            this.isSmString = false;

        } else if (type.equals("wbs")){

            this.name = rs.getString("wbs_name");
            this.shortName = rs.getString("wbs_short_name");
            this.id = rs.getInt("wbs_id");
            this.isTask = false;
            this.isSmString = false;
            this.parentId = rs.getInt("wbs_parent_id");
            this.high = "";
            this.taskUse = "";
            this.wbsLevel = 0;

        } else if (type.equals("eps")) {

            this.name = rs.getString("code")+" - "+rs.getString("name");
            this.shortName = rs.getString("code");
            this.id = rs.getInt("id");
            this.isTask = false;
            this.isSmString = false;

            this.high = "";
            this.taskUse = "";

            this.dbName = rs.getString("db_name");
            this.parentId = rs.getInt("parent_id");
            this.allowsChildren = true;

        } else if (type.equals("db")) {

            this.name = rs.getString("db_name");
            this.shortName = "";
            this.isTask = false;
            this.isSmString = false;
            this.high = "";
            this.taskUse = "";

            this.dbName = rs.getString("db_name");
            this.allowsChildren = true;

        } else if (type.equals("project")) {

            this.name = rs.getString("proj_short_name")+" - "+rs.getString("proj_name");
            this.shortName = rs.getString("proj_short_name");
            this.id = rs.getInt("proj_id");
            this.isTask = true;
            this.isSmString = false;
            this.high = "";
            this.taskUse = "";

            this.parentId = rs.getInt("eps_id");
            this.dbName = rs.getString("db_name");
            this.wbsId = rs.getInt("proj_wbs_id");
            this.allowsChildren = false;
            this.projCode = rs.getString("proj_code");

        }

    }

    //EPSNode constructor
    public ProjectNode( String name ) {

        this.name = name;
        this.shortName = "";
        this.isTask = false;
        this.isSmString = false;
        this.setAllowChildren(true);
        this.high = "";
        this.taskUse = "";

    }

    public ProjectNode( String name, String shortName, int id, Boolean isTask ) {

        this.name = name;
        this.shortName = shortName;
        this.id = id;
        this.isTask = isTask;
        this.isSmString = false;
        this.high = "";
        this.taskUse = "";

    }

    //wbsNode constructor
    public ProjectNode( String name, String shortName, int id, int parentId, Boolean isTask ) {

        this.name = name;
        this.shortName = shortName;
        this.id = id;
        this.isTask = isTask;
        this.isSmString = false;
        this.parentId = parentId;
        this.high = "";
        this.taskUse = "";
    }

    //taskNode constructor
    public ProjectNode( String name, String shortName, int id, int parentId, Boolean isTask, Double pct, String taskCodeValue, Date actualStartDate, Date actualFinishDate )
    {

        this.name = name;
        this.shortName = shortName;
        this.id = id;
        this.isTask = isTask;
        this.isSmString = false;
        this.taskCompletePct = pct;
        this.parentId = parentId;
        this.taskCodeValue = taskCodeValue;
        this.startDate = actualStartDate;
        this.finishDate = actualFinishDate;
        this.isSmString = false;
        this.high = "";
        this.taskUse = "";

    }

    //smetInTaskNodeForKS
    public ProjectNode (String smrNum, String smrName, String smrSmNum, String smrUnit, Double smrSmrVol, Double smrLinkVol, Boolean isPV, Double PV, Double smrMrVol, Double smrMechVol, Double smrTVol) {
        this.name = smrNum;
        this.smrName = smrName;
        this.smrSmNum = smrSmNum;
        this.smrUnit = smrUnit;
        this.smrSmrVol = smrSmrVol;
        this.smrLinkVol = smrLinkVol;
        this.isSmString = true;
        this.high = "";
        this.taskUse = "";
        this.isPV = isPV;
        this.PV = PV;
        this.smrMrVol = smrMrVol;
        this.smrMechVol = smrMechVol;
        this.smrTVol = smrTVol;
    }

    //smetStringNode constructor
/*    public ProjectNode( String name, String shortName, int id, Boolean isTask )
    {

        this.name = name;
        this.shortName = shortName;
        this.id = id;
        this.isTask = isTask;
        this.isSmString = false;

    }*/

    public ArrayList<SQLTaskRsrc> getRsrc () { return rsrc; }

    public void setTaskCompletePct ( Double pct ) { this.taskCompletePct = pct; }

    public Double getTaskCompletePct ( ) { return taskCompletePct; }

    public String getShortName() { return shortName; }

    public int getId() { return id; }

    public void setId( int id) { this.id = id; }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

    public int getParentId() {
       return parentId;
    }

    public void setHigh(String flag) {
        this.high = flag;
    }

    public String getHigh() {
        return high;
    }

    public void setTaskUse(String taskUse) {
        this.taskUse = taskUse;
    }

    public String getTaskUse() {
        return taskUse;
    }

    public String getName() { return name; }

    public void setName( String name ) { this.name = name; }

    public void setAllowChildren ( Boolean val ) { super.allowsChildren = val; }

    public boolean isTask() { return isTask; }

    public void setIsTask(Boolean isTask) { this.isTask = isTask; }

    public boolean isSmString() { return isSmString; }

    public String getDbName() { return dbName; }

    public void setDbName( String dbName ) { this.dbName = dbName; }

    @Override
    public String toString() { return name; }

    public List<ProjectNode> getChildren() {return children; }

    public int getWbsId() { return wbsId; }

    public void setWbsId(int wbsId) { this.wbsId = wbsId; }

    public String getTaskCodeValue() { return taskCodeValue; }

    public void setTaskCodeValue(String taskCodeValue) { this.taskCodeValue = taskCodeValue; }

    public String getUomValue() {
        return uomValue;
    }

    public void setUomValue(String uomValue) {
        this.uomValue = uomValue;
    }

    public String getPvValue() {
        return pvValue;
    }

    public void setPvValue(String pvValue) {
        this.pvValue = pvValue;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getFinishDate() {
        return finishDate;
    }

    public void setFinishDate(Date finishDate) {
        this.finishDate = finishDate;
    }

    public String getSmrNum() {
        return smrNum;
    }

    public void setSmrNum(String smrNum) {
        this.smrNum = smrNum;
    }

    public String getSmrName() {
        return smrName;
    }

    public void setSmrName(String smrName) {
        this.smrName = smrName;
    }

    public String getSmrSmNum() {
        return smrSmNum;
    }

    public void setSmrSmNum(String smrSmNum) {
        this.smrSmNum = smrSmNum;
    }

    public String getSmrUnit() {
        return smrUnit;
    }

    public void setSmrUnit(String smrUnit) {
        this.smrUnit = smrUnit;
    }

    public Double getSmrSmrVol() {
        return smrSmrVol;
    }

    public void setSmrSmrVol(Double smrSmrVol) {
        this.smrSmrVol = smrSmrVol;
    }

    public Double getSmrMrVol() {
        return smrMrVol;
    }

    public void setSmrMrVol(Double smrMrVol) {
        this.smrMrVol = smrMrVol;
    }

    public String getWDCode() {
        return WDCode;
    }

    public void setWDCode(String WDCode) {
        this.WDCode = WDCode;
    }

    public Double getPV() {
        return PV;
    }

    public void setPV(Double PV) {
        this.PV = PV;
    }

    public Boolean getIsPV() {
        return isPV;
    }

    public void setIsPV(Boolean isPV) {
        this.isPV = isPV;
    }

    public Double getSmrLinkVol() {
        return smrLinkVol;
    }

    public void setSmrLinkVol(Double smrLinkVol) {
        this.smrLinkVol = smrLinkVol;
    }

    public Double getSmrMechVol() {
        return smrMechVol;
    }

    public void setSmrMechVol(Double smrMechVol) {
        this.smrMechVol = smrMechVol;
    }

    public Double getSmrTVol() {
        return smrTVol;
    }

    public void setSmrTVol(Double smrTVol) {
        this.smrTVol = smrTVol;
    }

    public String getProjCode() {
        return projCode;
    }

    public void setProjCode(String projCode) {
        this.projCode = projCode;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getSector() {
        return sector;
    }

    public void setSector(String sector) {
        this.sector = sector;
    }

    public String getBlock() {
        return block;
    }

    public void setBlock(String block) {
        this.block = block;
    }

    public String getSubBlock() {
        return subBlock;
    }

    public void setSubBlock(String subBlock) {
        this.subBlock = subBlock;
    }

    public int getWbsLevel() {
        return wbsLevel;
    }

    public void setWbsLevel(int wbsLevel) {
        this.wbsLevel = wbsLevel;
    }

    public HashMap<String, SQLLink> getLinks() {
        return links;
    }

    public String getConstrCode() {
        return constrCode;
    }

    public void setConstrCode(String constrCode) {
        this.constrCode = constrCode;
    }
}
